﻿Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.Data.Sql
Imports Seuss.EnterpriseLibrary.Cryptograph
Imports Seuss.EnterpriseLibrary.DataAccess
Imports Seuss.EnterpriseLibrary.BusinessLogic

Imports System.Web
Imports System.Web.Security
Imports System.Configuration
Imports System.Data.SqlClient

Imports System.Data
Imports System.Collections.Generic

Partial Class Login
    Inherits System.Web.UI.Page
    Dim Db As Database = DatabaseFactory.CreateDatabase("CnSeuss")

    Protected Sub LoginButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginButton.Click        
        Dim IsApproved As Boolean = False
        Dim IsValid As Boolean = False
        Dim IsDatos As Boolean = False

        Me.UserName.Text = Me.UserName.Text.ToUpper
        Dim UserName As String = Me.UserName.Text
        Dim Password As String = Me.Password.Text
        Dim Redirect As String = Request.QueryString("ReturnUrl")


        Session("username") = Me.UserName.Text
        Session("Password") = Me.Password.Text

        If (String.IsNullOrEmpty(UserName) OrElse String.IsNullOrEmpty(Password)) Then
            Return
        End If

        Dim Usuario As New Seuss.EnterpriseLibrary.Types.Usuario(UserName, Password)

        Using Seguridad As New BLL_Administracion(Db)
            IsValid = Seguridad.Get_User_Validated(Nothing, Usuario)
        End Using

        If IsValid Then
            IsApproved = (Usuario.nPerUsuEstado > 0)
            If IsApproved Then
                Dim ticket As FormsAuthenticationTicket = New FormsAuthenticationTicket(2, UserName, DateTime.Now, DateTime.Now.AddMinutes(1440), True, _
                        Usuario.cPerCodigo, FormsAuthentication.FormsCookiePath)

                Dim encTicket As String = FormsAuthentication.Encrypt(ticket)
                Session("cPerCodigo") = Usuario.cPerCodigo
                Response.Cookies.Add(New HttpCookie(FormsAuthentication.FormsCookieName, encTicket))

                Dim Pagina As String = FormsAuthentication.GetRedirectUrl(UserName, True)
                Session("cPerCodigo") = Usuario.cPerCodigo

                Using Seguridad As New BLL_Administracion
                    Seguridad.Set_Transaccion_Insert(Nothing, 1008, Usuario.cPerCodigo, My.Request.UserHostAddress.ToString, CrearXMLTransaccion("LogIn", "/cPerCodigo|" + Trim(Usuario.cPerCodigo)))
                End Using

                If String.IsNullOrEmpty(Redirect) Then
                    Response.Redirect("~/Consulta/Perfil.aspx")
                Else
                    Response.Redirect(Redirect)
                End If

            Else
                FailureText.Text = "Usuario no válido"
            End If
        Else
            FailureText.Text = "Usuario y/o password incorrecto."
        End If
        Usuario = Nothing
        Return
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ''IDENTIFICANDO MOVIL
        'Dim navegador As String = Request.UserAgent
        'Dim dir_movil As String = "/campus/" 'A DONDE SE VA A REDIRECCIONAR SI ES UN MOBIL
        ''CONTAINS TAMBIEN FUNCIONA
        ''If BuscarTexto(navegador, "iPhone") Then
        'If BuscarTexto(navegador, "iPad") Then 'iPad PAGINA NORMAL
        '    Response.Redirect(dir_movil)
        'ElseIf BuscarTexto(navegador, "Tablet") Then   'Tablet
        '    Response.Redirect(dir_movil)
        'ElseIf BuscarTexto(navegador, "Opera Mo") Then   'Opera Mobile
        '    'Response.Redirect(dir_movil)
        'ElseIf navegador.Contains("iPhone") Then 'iPhone
        '    'Response.Redirect(dir_movil)
        'ElseIf navegador.Contains("BlackBerry") Then 'BlackBerry
        '    'Response.Redirect(dir_movil)
        'ElseIf navegador.Contains("Fennec") Then 'Firefox Movil
        '    'Response.Redirect(dir_movil)
        'ElseIf navegador.Contains("Android") Then 'Android o SmartPhone
        '    'Response.Redirect(dir_movil)
        'ElseIf navegador.Contains("Opera Mini") Then 'Opera Mini
        '    'Response.Redirect(dir_movil)
        'ElseIf navegador.Contains("Openwave") Then 'Openwave Navegadores Antiguos
        '    'Response.Redirect(dir_movil)
        'ElseIf navegador.Contains("Mobile") Then 'Otros Mobiles
        '    'Response.Redirect(dir_movil)
        'ElseIf navegador.Contains("Movil") Then 'Otros Mobiles Español
        '    'Response.Redirect(dir_movil)
        'ElseIf navegador.Contains("Palm") Then 'Palm mobile phones
        '    'Response.Redirect(dir_movil)
        'ElseIf navegador.Contains("Windows CE") Then 'Windows Mobile
        '    'Response.Redirect(dir_movil)
        'ElseIf navegador.Contains("Portable") Then 'PlayStation
        '    'Response.Redirect(dir_movil)
        'ElseIf navegador.Contains("iPod") Then 'iPod Dispositivos para escuchar musica
        '    'Response.Redirect(dir_movil)
        'ElseIf navegador.Contains("Nokia") Then 'Celulares Nokia
        '    'Response.Redirect(dir_movil)
        'ElseIf navegador.Contains("MOT") Then 'Celulares Motorola
        '    'Response.Redirect(dir_movil)
        'ElseIf navegador.Contains("SAMSUNG") Then 'Celulares SAMSUNG
        '    'Response.Redirect(dir_movil)
        'ElseIf navegador.Contains("SonyEricsson") Then 'Celulares SonyEricsson
        '    'Response.Redirect(dir_movil)
        'ElseIf navegador.Contains("PPC") Then 'PPC Windows Mobile
        '    'Response.Redirect(dir_movil)
        'ElseIf navegador.Contains("Huawei") Then 'Celulares Huawei
        '    'Response.Redirect(dir_movil)
        'Else
        '    Response.Redirect(dir_movil)
        'End If

    End Sub

    'Private Function BuscarTexto(ByVal Texto As String, ByVal Busqueda As String) As Boolean
    '    Dim i As Integer
    '    i = InStr(1, Texto, Busqueda)
    '    If i > 0 Then
    '        BuscarTexto = True
    '    Else
    '        BuscarTexto = False
    '    End If
    'End Function

    Public Function CrearXMLTransaccion(ByVal Descripcion As String, ByVal Campos As String) As String
        '//los valor en campo llegan asi: /campo|valor/campo|valor
        Dim VXML As String
        Dim a As String() = Campos.Split(New Char() {"/"c})
        Dim s As String
        VXML = "<Transaccion>"
        VXML = VXML + "<Descripcion>" + Trim(Descripcion) + "</Descripcion>"
        For Each s In a
            If s.Trim().Length > 0 Then
                Dim c As String() = s.Split(New Char() {"|"c})
                Dim Campo As String = Trim(CStr(c(0)))
                Dim valor As String = Trim(CStr(c(1)))
                VXML = VXML + "<" + Trim(Campo) + ">" + Trim(valor) + "</" + Trim(Campo) + ">"
            End If
        Next
        VXML = VXML + "</Transaccion>"
        Return VXML
    End Function
    'End Sub

    Protected Sub Password_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Password.TextChanged

    End Sub
End Class
